
// includes, system
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <cuda.h>
#include <cuda_runtime_api.h>
// includes, kernels


#include <errno.h>

#define _CRT_SECURE_NO_DEPRECATE

#define a1  -39.6968302866538f
#define a2  220.946098424521f
#define a3  -275.928510446969f
#define a4  138.357751867269f
#define a5  -30.6647980661472f
#define a6  2.50662827745924f
#define b1  -54.4760987982241f
#define b2  161.585836858041f
#define b3  -155.698979859887f
#define b4  66.8013118877197f
#define b5  -13.2806815528857f
#define c1  -7.78489400243029E-03f
#define c2  -0.322396458041136f
#define c3  -2.40075827716184f
#define c4  -2.54973253934373f
#define c5  4.37466414146497f
#define c6  2.93816398269878f
#define d1  7.78469570904146E-03f
#define d2  0.32246712907004f
#define d3  2.445134137143f
#define d4  3.75440866190742f

    #define a1__  2.50662823884f
    #define a2__  -18.61500062529f
    #define a3__  41.39119773534f
    #define a4__  -25.44106049637f
    #define b1__  -8.4735109309f
    #define b2__  23.08336743743f
    #define b3__  -21.06224101826f
    #define b4__  3.13082909833f
    #define c1__  0.337475482272615f
    #define c2__  0.976169019091719f
    #define c3__  0.160797971491821f
    #define c4__  2.76438810333863E-02f
    #define c5__  3.8405729373609E-03f
    #define c6__  3.951896511919E-04f
    #define c7__  3.21767881768E-05f
    #define c8__  2.888167364E-07f
    #define c9__  3.960315187E-07f

#define low  0.02425f
#define high 0.97575f

#ifdef _WIN32
    typedef   signed __int32  int32_t;
  typedef unsigned __int32 uint32_t;
  typedef   signed __int64  int64_t;
  typedef unsigned __int64 uint64_t;
#elif defined(linux)
  //  typedef   signed __int32  int32_t;
typedef unsigned  int  uint32_t;

//  typedef   signed __int64  int64_t;
typedef unsigned  long int  uint64_t;
#else
    #error "What's your operating system?"
#endif

 



#define _CRT_SECURE_NO_DEPRECATE
#define BLOCK_S 	13
#define pf 13
#define	N_okno		30
#define	N		150	// не может быть меньше 34
#define	N_1		149
#define h 0.92307692f /* это 6*r/13*/
#define h_2 0.46153846f
#define V 8.13159f
#define V_1 0.1229771791f
#define V_2 6.61227559281e1f
#define r 2.0f
#define r_2 0.4e1f
#define pi 3.141592653589793f
#define pi_con_2_13 0.4833219467f//2 * pi/13;
#define r1 0.24f
#define r0 0.24f
#define r0_4 5.96f
#define cos_angle 0.24f
#define sin_angle 0.24f
#define cospi 0.9709418174f//косинус пи на 13
#define sinpi 0.2393156642f// sin pi На 13
#define r1_1 4.16666666666666666667f
#define Rm 8.131590664720f
#define Rt 2.0f
  //#define k 292.0f*305.16f*1.3806504e-5f// константа вертикального взаимодействия 7,3ккал
#define B 3000.0f*305.16f*1.3806504e-5f// константа вертикального взаимодействия 12,63295116
//#define B_2__ 25.26590232f
//#define A 100.0f*305.16f*1.3806504e-5f// константа горизонтального взаимодействия к*3= 36.156950*305.16*1.3806504e-5
//#define A_1___ 4.21319276064f//0.842638552128f//0,421319276064 = 100KT
//#define T0 0.2f // константа вертикального взаимодействия
#define g1 3.0e-9f// константа для декартовых взаимодействия eta=0.004 пуаз  3.0e-10
#define g2 6.5e-8f// константа для угла взаимодействия 6.5e-9
//#define g1_1 0.3333333333e9f// константа для декартовых взаимодействия eta=0.004 пуаз  3.0e-10
//#define g2_1 0.1538461538e8f// константа для угла взаимодействия 6.5e-9
//#define dt 1.0e-10f
#define dt_g1_1 0.3333333333e-2f
#define dt_g2_1 0.1538461538e-3f
#define T 305.0f//305.0f //305.16f //температура
//#define K 1.3806504e-5f //константа больцмана 10^9*10^9
#define N_OUT_ALL 5000
#define NfoPr 10000000
#define Kbol 1.3806504e-5f //константа больцмана 10^9*10^9
#define DT 1.0e-10f
#define Gamma 0.3e+9f
#define Gammay 0.3e+8f
#define A 4.3f//lateral * 237.5 = kt   33.5
#define K 4.4f//longitudal podelit 300 na 1.38-5 ///МЕНЯТЬ НАДО ОБА
#define istreh 0.577f
#define w 3.52f
#define _wo 4.166666666666667f// =1/0.24f
#define new_AA 0.07f
#define per_ex 0.01f
#define v_ex 0.1f

#define Ro 0.24f//lateral
#define r12 1.0f
#define r04 1.5f
#define Ao 0.0f
#define Bo 0.2f
#define Go 0.0f
#define Ka 10.24f
#define Kb 0.64f
#define Kg 10.24f
#define K_2a 20.48f//МЕНЯТЬ НАДО ОБА ЭТО Ka*2!!!!!!!!
#define K_2b 1.28f//МЕНЯТЬ НАДО ОБА ЭТО Kb *2!!!!!!!!
#define K_2g 20.48f//МЕНЯТЬ НАДО ОБА ЭТО Kg *2!!!!!!!!


////////////////////////////random//////////////////////////////

__global__ void InitRandom(int * rand_seed,uint32_t * in1,uint32_t * in2,uint32_t * in3,uint32_t * in4,uint32_t * in5)
{
	//задает начальные ккординаты.
	int bx=blockIdx.x;
	int tx=threadIdx.x;
	int index=(bx+tx*pf);
	long s=rand_seed[index];
	s = s * 29943829 - 1;
	in1[index] = s;
	s = s * 29943829 - 1;
    in2[index] = s;
	s = s * 29943829 - 1;
    in3[index] = s;
	s = s * 29943829 - 1;
    in4[index] = s;
	s = s * 29943829 - 1;
    in5[index] = s;

}
__device__ float Random_New(uint32_t * in1,uint32_t * in2,uint32_t * in3,uint32_t * in4,uint32_t * in5)
{

	      float y, z;

  uint64_t sum;
sum = (uint64_t)2111111111UL * (uint64_t)in4[0]+(uint64_t)1492*(uint64_t)(in3[0])+(uint64_t)1776*(uint64_t)(in2[0])+(uint64_t)5115*(uint64_t)(in1[0])+(uint64_t)in5[0];
  in4[0] = in3[0];
  in3[0] = in2[0];
  in2[0] = in1[0];
  in5[0] = (uint32_t)(sum >> 32);                  // Carry
  in1[0] = (uint32_t)sum;                          // Low 32 bits of sum
  float P=(float)in1[0]*(1.0f/(4294967296.0f));





    if(P <= 0.0f || P >= 1.0f)
	{
//        return __int_as_float(0x7FFFFFFF);
		return 0.0f;
	}
	else
	{
    y = P - 0.5f;
    if(fabsf(y) < 0.42f){
        z = y * y;
        z = y * (((a4__ * z + a3__) * z + a2__) * z + a1__) / ((((b4__ * z + b3__) * z + b2__) * z + b1__) * z + 1.0f);
    }else{
        if(y > 0.0f)
            z = __logf(-__logf(1.0f - P));
        else
            z = __logf(-__logf(P));

        z = c1__ + z * (c2__ + z * (c3__ + z * (c4__ + z * (c5__ + z * (c6__ + z * (c7__ + z * (c8__ + z * c9__)))))));
        if(y < 0.0f) z = -z;
    }
	}

    return z;

}
__device__ float Random_fast(uint32_t * in1,uint32_t * in2,uint32_t * in3,uint32_t * in4,uint32_t * in5)
{
   float z, R;
  uint64_t sum;
  sum = (uint64_t)2111111111UL * (uint64_t)in4[0] +
     (uint64_t)1492 * (uint64_t)(in3[0]) +
     (uint64_t)1776 * (uint64_t)(in2[0]) +
     (uint64_t)5115 * (uint64_t)(in1[0]) +
     (uint64_t)in5[0];
  in4[0] = in3[0];  in3[0] = in2[0]; in2[0] = in1[0];
  in5[0] = (uint32_t)(sum >> 32);                  // Carry
  in1[0] = (uint32_t)sum;                          // Low 32 bits of sum
  float P=(float)in1[0]*(1.0f/(4294967296.0f));



    if(P <= 0.0f || P >= 1.0f)
	{
return 0.0f;
	}
	else
	{
    if(P < low){
        z = sqrtf(-2.0f * __logf(P));
        z = (((((c1 * z + c2) * z + c3) * z + c4) * z + c5) * z + c6) /
            ((((d1 * z + d2) * z + d3) * z + d4) * z + 1.0f);
    }else{
        if(P > high){
            z = sqrtf(-2.0f * __logf(1.0f - P));
            z = -(((((c1 * z + c2) * z + c3) * z + c4) * z + c5) * z + c6) /
                 ((((d1 * z + d2) * z + d3) * z + d4) * z + 1.0f);
        }else{
            z = P - 0.5f;
            R = z * z;
            z = (((((a1 * R + a2) * R + a3) * R + a4) * R + a5) * R + a6) * z /
                (((((b1 * R + b2) * R + b3) * R + b4) * R + b5) * R + 1.0f);
        }
    }
	}

	return z;
}
__global__ void DoRandom(uint32_t * in1,uint32_t * in2,uint32_t * in3,uint32_t * in4,uint32_t * in5,float *rand_out)
{
	int bx=blockIdx.x;
	int tx=threadIdx.x;
	int index=(bx+tx*pf);

    float z, R;
  uint64_t sum;
  sum = (uint64_t)2111111111UL * (uint64_t)in4[index] +
     (uint64_t)1492 * (uint64_t)(in3[index]) +
     (uint64_t)1776 * (uint64_t)(in2[index]) +
     (uint64_t)5115 * (uint64_t)(in1[index]) +
     (uint64_t)in5[index];
  in4[index] = in3[index];  in3[index] = in2[index]; in2[index] = in1[index];
  in5[index] = (uint32_t)(sum >> 32);                  // Carry
  in1[index] = (uint32_t)sum;                          // Low 32 bits of sum
  float P=(float)in1[index]*(1.0f/(4294967296.0f));

    if(P <= 0.0f || P >= 1.0f)
	{
rand_out[index]=0.0f;
	}
	else
	{
    if(P < low){
        z = sqrtf(-2.0f * logf(P));
        z = (((((c1 * z + c2) * z + c3) * z + c4) * z + c5) * z + c6) /
            ((((d1 * z + d2) * z + d3) * z + d4) * z + 1.0f);
    }else{
        if(P > high){
            z = sqrtf(-2.0f * logf(1.0f - P));
            z = -(((((c1 * z + c2) * z + c3) * z + c4) * z + c5) * z + c6) /
                 ((((d1 * z + d2) * z + d3) * z + d4) * z + 1.0f);
        }else{
            z = P - 0.5f;
            R = z * z;
            z = (((((a1 * R + a2) * R + a3) * R + a4) * R + a5) * R + a6) * z /
                (((((b1 * R + b2) * R + b3) * R + b4) * R + b5) * R + 1.0f);
        }
    }
	}
	rand_out[index] =z;
}
__global__ void DoRandom_new(uint32_t * in1,uint32_t * in2,uint32_t * in3,uint32_t * in4,uint32_t * in5,float *rand_out)
{
	int bx=blockIdx.x;
	int tx=threadIdx.x;
	int index=(bx+tx*pf);

      float y, z;
	  uint64_t sum;
  sum = (uint64_t)2111111111UL * (uint64_t)in4[index] +
     (uint64_t)1492 * (uint64_t)(in3[index]) +
     (uint64_t)1776 * (uint64_t)(in2[index]) +
     (uint64_t)5115 * (uint64_t)(in1[index]) +
     (uint64_t)in5[index];
  in4[index] = in3[index];  in3[index] = in2[index]; in2[index] = in1[index];
  in5[index] = (uint32_t)(sum >> 32);                  // Carry
  in1[index] = (uint32_t)sum;                          // Low 32 bits of sum
  float P=(float)in1[index]*(1.0f/(4294967296.0f));

    if(P <= 0.0f || P >= 1.0f)
	{
        rand_out[index] = __int_as_float(0x7FFFFFFF);
	}
	else
	{
    y = P - 0.5f;
    if(fabsf(y) < 0.42f){
        z = y * y;
        z = y * (((a4__ * z + a3__) * z + a2__) * z + a1__) / ((((b4__ * z + b3__) * z + b2__) * z + b1__) * z + 1.0f);
    }else{
        if(y > 0.0f)
            z = __logf(-__logf(1.0f - P));
        else
            z = __logf(-__logf(P));

        z = c1__ + z * (c2__ + z * (c3__ + z * (c4__ + z * (c5__ + z * (c6__ + z * (c7__ + z * (c8__ + z * c9__)))))));
        if(y < 0.0f) z = -z;
    }
	}

    rand_out[index] = z;
}

__device__ inline float MoroInvCNDgpu(float P){

    float y, z;

    if(P <= 0.0f || P >= 1.0f)
        return __int_as_float(0x7FFFFFFF);

    y = P - 0.5f;
    if(fabsf(y) < 0.42f){
        z = y * y;
        z = y * (((a4__ * z + a3__) * z + a2__) * z + a1__) / ((((b4__ * z + b3__) * z + b2__) * z + b1__) * z + 1.0f);
    }else{
        if(y > 0.0f)
            z = __logf(-__logf(1.0f - P));
        else
            z = __logf(-__logf(P));

        z = c1__ + z * (c2__ + z * (c3__ + z * (c4__ + z * (c5__ + z * (c6__ + z * (c7__ + z * (c8__ + z * c9__)))))));
        if(y < 0.0f) z = -z;
    }

    return z;
}

  //////////////////////////////////////////////////////////////////
  //ref_md
  /////////////////////////////////////////////////////////////////
  typedef struct  {
	float x, y, z,a;
} vec;

__constant__ float cf_angle_[pf];
__constant__ float sf_angle_[pf];

__constant__ unsigned int right__[pf*	N];
__constant__ unsigned int left__[pf*	N];
__constant__ unsigned int up__[pf*	N];
__constant__ unsigned int down__[pf*	N];
__constant__ unsigned int uT__[pf*	N];
__constant__ unsigned int dT__[pf*	N];
__constant__ unsigned int lT__[pf*	N];
__constant__ unsigned int rT__[pf*	N];
__constant__ float isup[N];
__constant__ float koeff[1];
__constant__ float koeff2[1];
__constant__ float koren[1];
__constant__ int RAMKA[13];
void SetConstMem( )
{
	float _cf_angle_[pf];
	float _sf_angle_[pf];
	float _angle[pf];
	float _isup[N];
	int _RAMKA[13];
	unsigned int _right__[pf*	N];
	unsigned int _left__[pf*	N];
	unsigned int _up__[pf*	N];
	unsigned int _down__[pf*	N];
	unsigned int _uT__[pf*	N];
	unsigned int _dT__[pf*	N];
	unsigned int _lT__[pf*	N];
	unsigned int _rT__[pf*	N];
	float _koeff=sqrtf(2.0f*Kbol*T*DT/g1);
	float _koeff2=sqrtf(2.0f*Kbol*T*DT/g2);
	float _koren=sqrtf(Rt*Rt-h*h/4.0f);
	int pitch= pf;

	for(int tx=0;tx<pf;tx++)
	{
		_angle[tx] = (float)tx  * pi_con_2_13;
		_cf_angle_[tx]=cosf(_angle[tx]);
		_sf_angle_[tx]=sinf(_angle[tx]);

	}
	for(int i=0;i<N;i++)
	{
		_isup[i]=1.0f;
	}
	_isup[N-1]=0;

	for (int i=0;i<pf;i++)
	{
		for(int j=0;j<N;j++)
		{
			_up__[i+pitch *j]=2*(i+pitch*(j))+1;//ссылка верхний центр взаиможейстивия с низу
			_down__[i+pitch *j]=2*(i+pitch *(j));
			_right__[i+pitch *j]=2*(i+pitch *j);
			_left__[i+pitch *j]=2*(i+pitch *j)+1;
			_uT__[i+pitch *j]=(i+pitch *(j));
			_dT__[i+pitch *j]=(i+pitch *(j));
			_rT__[i+pitch *j]=(i+pitch *(j));
			_lT__[i+pitch *j]=(i+pitch *(j));
		}
	}
	for (int i=1;i<pf-1;i++)
	{
		for(int j=1;j<N-1;j++)
		{
			_up__[i+pitch *j]=2*(i+pitch*(j-1));//ссылка верхний центр взаиможейстивия с низу
			_down__[i+pitch *j]=2*(i+pitch *(j+1))+1;
			_right__[i+pitch *j]=2*(i+1+pitch *j)+1;
			_left__[i+pitch *j]=2*(i-1+pitch *j);
			_uT__[i+pitch *j]=(i+pitch *(j-1));
			_dT__[i+pitch *j]=(i+pitch *(j+1));
			_rT__[i+pitch *j]=(i+1+pitch *(j));
			_lT__[i+pitch *j]=(i-1+pitch *(j));
		}
	}

	for (int i=1;i<12;i++)
	{
		_up__[i+pitch *0]=2*(i+pitch*(0))+1;//ссылка верхний центр взаиможейстивия с низу для 0 ряда - егоже нижний
		_down__[i+pitch *0]=2*(i+pitch *(0+1))+1;//ссылка на нижний центр взаимодействия сверху
		_right__[i+pitch *0]=2*(i+1+pitch *0)+1;//ссылка на правый центр шара слева
		_left__[i+pitch *0]=2*(i-1+pitch *0);
			_rT__[i+pitch *0]=(i+1+pitch *0);
		_lT__[i+pitch *0]=(i-1+pitch *0);

		_uT__[i+pitch *0]=(i+pitch *0);
		_dT__[i+pitch *0]=(i+pitch *1);


		_up__[i+pitch *(N-1)]=2*(i+pitch*(N-1-1));//ссылка верхний центр взаиможейстивия с низу
		_down__[i+pitch *(N-1)]=2*(i+pitch *(N-1));//ссылка на нижний центр взаимодействия сверху для N ряда - его же верхнйи
		_right__[i+pitch *(N-1)]=2*(i+1+pitch *(N-1))+1;//ссылка на правый центр шара слева
		_left__[i+pitch *(N-1)]=2*(i-1+pitch *(N-1));
			_rT__[i+pitch *(N-1)]=(i+1+pitch *(N-1));
		_lT__[i+pitch *(N-1)]=(i-1+pitch *(N-1));

		_uT__[i+pitch *(N-1)]=(i+pitch *(N-1-1));
		_dT__[i+pitch *(N-1)]=(i+pitch *(N-1));
	}

	for(int j=1;j<N-1;j++)
	{
		_up__[0+pitch *j]=2*(0+pitch*(j-1));//ссылка верхний центр взаиможейстивия с низу
		_down__[0+pitch *j]=2*(0+pitch *(j+1))+1;
		_uT__[0+pitch *j]=(0+pitch *(j-1));
		_dT__[0+pitch *j]=(0+pitch *(j+1));

		_up__[12+pitch *j]=2*(12+pitch*(j-1));//ссылка верхний центр взаиможейстивия с низу
		_down__[12+pitch *j]=2*(12+pitch *(j+1))+1;
		_uT__[12+pitch *j]=(12+pitch *(j-1));
		_dT__[12+pitch *j]=(12+pitch *(j+1));

		if((j>=3)&&(j<(N-3)))
		{
			_right__[0+pitch *j]=2*(1+pitch *j)+1;//ссылка на правый центр шара слева
			_left__[0+pitch *j]=2*(12+pitch *(j-3));//левый цент шара справа

			_right__[12+pitch *j]=2*(0+pitch *(j+3))+1;//ссылка на правый центр шара слева
			_left__[12+pitch *j]=2*(11+pitch *j);
				_rT__[0+pitch *j]=(1+pitch *j);
			_lT__[0+pitch *j]=(12+pitch *(j-3));

			_rT__[12+pitch *j]=(0+pitch *(j+3));
			_lT__[12+pitch *j]=(11+pitch *j);
		}
		else
		{
			if(j<3)
			{
				_right__[0+pitch *j]=2*(1+pitch *j)+1;//ссылка на правый центр шара слева
				_left__[0+pitch *j]=2*(0+pitch *(j))+1;//левый цент шара справа в этом случае правый цента этого же шара

				_right__[12+pitch *j]=2*(0+pitch *(j+3))+1;//ссылка на правый центр шара слева
				_left__[12+pitch *j]=2*(11+pitch *j);
					_rT__[0+pitch *j]=(1+pitch *j);
				_lT__[0+pitch *j]=(0+pitch *(j));

				_rT__[12+pitch *j]=(0+pitch *(j+3));
				_lT__[12+pitch *j]=(11+pitch *j);
			}
			if(j>=(N-3))
			{
				_right__[0+pitch *j]=2*(1+pitch *j)+1;//ссылка на правый центр шара слева
				_left__[0+pitch *j]=2*(12+pitch *(j-3));//левый цент шара справа

				_right__[12+pitch *j]=2*(12+pitch *j);//ссылка на правый центр шара слева
				_left__[12+pitch *j]=2*(11+pitch *j);
					_rT__[0+pitch *j]=(1+pitch *j);
				_lT__[0+pitch *j]=(12+pitch *(j-3));

				_rT__[12+pitch *j]=(12+pitch *j);
				_lT__[12+pitch *j]=(11+pitch *j);
			}
		}

	}

	_up__[0+pitch *0]=2*(0+pitch*(0))+1;//ссылка верхний центр взаиможейстивия с низу для 0 ряда - егоже нижний
	_down__[0+pitch *0]=2*(0+pitch *(0+1))+1;//ссылка на нижний центр взаимодействия сверху
	_right__[0+pitch *0]=2*(0+1+pitch *0)+1;//ссылка на правый центр шара слева
	_left__[0+pitch *0]=2*(0+pitch *0)+1;
	_rT__[0+pitch *0]=(0+1+pitch *0);
	_lT__[0+pitch *0]=(0+pitch *0);
	_uT__[0+pitch *0]=(0+pitch *0);
	_dT__[0+pitch *0]=(0+pitch *1);


	_up__[12+pitch *(N-1)]=2*(12+pitch*(N-1-1));//ссылка верхний центр взаиможейстивия с низу
	_down__[12+pitch *(N-1)]=2*(12+pitch *(N-1));//ссылка на нижний центр взаимодействия сверху для N ряда - его же верхнйи
	_right__[12+pitch *(N-1)]=2*(12+pitch *(N-1));//ссылка на правый центр шара слева
	_left__[12+pitch *(N-1)]=2*(12-1+pitch *(N-1));
		_rT__[12+pitch *(N-1)]=(12+pitch *(N-1));
	_lT__[12+pitch *(N-1)]=(12-1+pitch *(N-1));


	_uT__[12+pitch *(N-1)]=(12+pitch *(N-1-1));
	_dT__[12+pitch *(N-1)]=(12+pitch *(N-1));


	_up__[12+pitch *(0)]=2*(12+pitch*(0));//ссылка верхний центр взаиможейстивия с низу
	_down__[12+pitch *(0)]=2*(12+pitch *(0+1))+1;//ссылка на нижний центр взаимодействия сверху для N ряда - его же верхнйи
		_rT__[12+pitch *(0)]=(0+pitch *(0+3));//пїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅ
	_lT__[12+pitch *(0)]=(12-1+pitch *(0));
	_right__[12+pitch *(0)]=2*(0+pitch *(0+3))+1;//ссылка на правый центр шара слева
	_left__[12+pitch *(0)]=2*(12-1+pitch *(0));

	_uT__[12+pitch *(0)]=(12+pitch *(0));
	_dT__[12+pitch *(0)]=(12+pitch *(0+1));

	_up__[0+pitch *(N-1)]=2*(0+pitch*(N-1-1));//ссылка верхний центр взаиможейстивия с низу
	_down__[0+pitch *(N-1)]=2*(0+pitch *(N-1));//ссылка на нижний центр взаимодействия сверху для N ряда - его же верхнйи
	_right__[0+pitch *(N-1)]=2*(1+pitch *(N-1))+1;//ссылка на правый центр шара слева
	_left__[0+pitch *(N-1)]=2*(12+pitch *(N-1-3));
		_rT__[0+pitch *(N-1)]=(1+pitch *(N-1));//пїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅ
	_lT__[0+pitch *(N-1)]=(12+pitch *(N-1-3));


	_uT__[0+pitch *(N-1)]=(0+pitch *(N-1-1));
	_dT__[0+pitch *(N-1)]=(0+pitch *(N-1));



	cudaMemcpyToSymbol(cf_angle_, _cf_angle_, sizeof(_cf_angle_));
	cudaMemcpyToSymbol(sf_angle_, _sf_angle_, sizeof(_sf_angle_));
	cudaMemcpyToSymbol(right__, _right__, sizeof(_right__));
	cudaMemcpyToSymbol(left__, _left__, sizeof(_left__));
	cudaMemcpyToSymbol(up__, _up__, sizeof(_up__));
	cudaMemcpyToSymbol(down__, _down__, sizeof(_down__));
	cudaMemcpyToSymbol(uT__, _uT__, sizeof(_uT__));
	cudaMemcpyToSymbol(dT__, _dT__, sizeof(_dT__));
	cudaMemcpyToSymbol(rT__, _rT__, sizeof(_rT__));
	cudaMemcpyToSymbol(lT__, _lT__, sizeof(_lT__));
	cudaMemcpyToSymbol(isup, _isup, sizeof(_isup));
	cudaMemcpyToSymbol(koeff, &_koeff, sizeof(_koeff));
	cudaMemcpyToSymbol(koeff2, &_koeff2, sizeof(_koeff2));
	cudaMemcpyToSymbol(koren, &_koren, sizeof(_koren));
	//cudaMemcpyToSymbol(RAMKA, _RAMKA, sizeof(_RAMKA));
	cudaThreadSynchronize();
}



__global__ void CoordXYZABGDefaulteKernel( float *x, float *y, float *z,float *a, float *b, float*g)
{
	//задает начальные ккординаты. bx - Номер протофиламента, tx- номер мономера в протофиламенте
	int bx=blockIdx.x;
	int tx=threadIdx.x;
	int index_2=(bx+tx*pf);
	x[index_2]=Rm*cf_angle_[bx];
	y[index_2]=-Rm*sf_angle_[bx];
	z[index_2]=h*bx+2.0f*Rt*tx;
	a[index_2]=0.0f;
	b[index_2]=0.0f;
	g[index_2]=0.0f;
	__syncthreads();

}
__global__ void NewKernel_KOOR(float*__x,float*__y,float*__z,float*__a,float*__b,float*__g,vec* left_right,vec* up_down)
{//запускаеться для pf блока из 32 нитий.
//считает координаты центров взаимодействия
//bx  номер протофиламента tx номер мономера в протофиламете или номер слоя

	int bx=blockIdx.x;
	int tx=threadIdx.x;

	int index=2*(bx+tx*pf);
	int index_2=(bx+tx*pf);
	float sina;
	float cosa;
	float cosb;
	float sinb;
	float cosg;
	float sing;
	float cosw;
	float sinw;



	float x=__x[index_2];
	float y=__y[index_2];
	float z=__z[index_2];
	float a=__a[index_2];
	float b=__b[index_2];
	float g=__g[index_2];

//h_c=h_2;
cosw=cf_angle_[bx];
sinw=sf_angle_[bx];
sina=sinf(a);
sinb=sinf(b);
sing=sinf(g);
cosa=cosf(a);
cosb=cosf(b);
cosg=cosf(g);

//down
up_down[index+1].x=((0*cosb+Rt*sinb)*cosg+0*sing)*cosw+(cosa*(-(0*cosb+Rt*sinb)*sing+0*cosg)+sina*(0*sinb+(-Rt)*cosb))*sinw+x;
up_down[index+1].y=-((0*cosb+Rt*sinb)*cosg+0*sing)*sinw+(cosa*(-(0*cosb+Rt*sinb)*sing+0*cosg)+sina*(0*sinb+(-Rt)*cosb))*cosw+y;
up_down[index+1].z=(-sina*(-(0*cosb+Rt*sinb)*sing+0*cosg)+cosa*(0*sinb+(-Rt)*cosb))+z;
//up
up_down[index].x=((0*cosb-Rt*sinb)*cosg+0*sing)*cosw+(cosa*(-(0*cosb-Rt*sinb)*sing+0*cosg)+sina*(0*sinb+Rt*cosb))*sinw+x;
up_down[index].y=-((0*cosb-Rt*sinb)*cosg+0*sing)*sinw+(cosa*(-(0*cosb-Rt*sinb)*sing+0*cosg)+sina*(0*sinb+Rt*cosb))*cosw+y;
up_down[index].z=(-sina*(-(0*cosb+Rt*sinb)*sing+0*cosg)+cosa*(0*sinb+Rt*cosb))+z;

//right

left_right[index+1].x=(((-koren[0]*sinpi)*cosb+h_2*sinb)*cosg+koren[0]*cospi*sing)*cosw+(cosa*(-((-koren[0]*sinpi)*cosb+h_2*sinb)*sing+koren[0]*cospi*cosg)+sina*((-koren[0]*sinpi)*sinb-h_2*cosb))*sinw+x;
left_right[index+1].y=-(((-koren[0]*sinpi)*cosb+h_2*sinb)*cosg+koren[0]*cospi*sing)*sinw+(cosa*(-((-koren[0]*sinpi)*cosb+h_2*sinb)*sing+koren[0]*cospi*cosg)+sina*((-koren[0]*sinpi)*sinb-h_2*cosb))*cosw+y;
left_right[index+1].z=(-sina*(-((-koren[0]*sinpi)*cosb+h_2*sinb)*sing+koren[0]*cospi*cosg)+cosa*((-koren[0]*sinpi)*sinb-h_2*cosb))+z;
//left
left_right[index].x=(((-koren[0]*sinpi)*cosb-h_2*sinb)*cosg+(-koren[0]*cospi)*sing)*cosw+(cosa*(-((-koren[0]*sinpi)*cosb-h_2*sinb)*sing+(-koren[0]*cospi)*cosg)+sina*((-koren[0]*sinpi)*sinb+h_2*cosb))*sinw+x;
left_right[index].y=-(((-koren[0]*sinpi)*cosb-h_2*sinb)*cosg+(-koren[0]*cospi)*sing)*sinw+(cosa*(-((-koren[0]*sinpi)*cosb-h_2*sinb)*sing+(-koren[0]*cospi)*cosg)+sina*((-koren[0]*sinpi)*sinb+h_2*cosb))*cosw+y;
left_right[index].z=(-sina*(-((-koren[0]*sinpi)*cosb-h_2*sinb)*sing+(-koren[0]*cospi)*cosg)+cosa*((-koren[0]*sinpi)*sinb+h_2*cosb))+z;


}


/*
	__shared__ float Rr_x[N_okno];
	__shared__ float Rr_y[N_okno];
	__shared__ float Rr_z[N_okno];
	__shared__ float Rl_x[N_okno];
	__shared__ float Rl_y[N_okno];
	__shared__ float Rl_z[N_okno];
	__shared__ float Rv_x[N_okno];
	__shared__ float Rv_y[N_okno];
	__shared__ float Rv_z[N_okno];
	__shared__ float Rn_x[N_okno];
	__shared__ float Rn_y[N_okno];
	__shared__ float Rn_z[N_okno];
	__shared__ float Xr_b[N_okno];
	__shared__ float Yr_b[N_okno];
	__shared__ float Zr_b[N_okno];
	__shared__ float Xl_b[N_okno];
	__shared__ float Yl_b[N_okno];
	__shared__ float Zl_b[N_okno];
	__shared__ float Xv_b[N_okno];
	__shared__ float Yv_b[N_okno];
	__shared__ float Zv_b[N_okno];
	__shared__ float Xn_b[N_okno];
	__shared__ float Yn_b[N_okno];
	__shared__ float Zn_b[N_okno];
	__shared__ float Xr_a[N_okno];
	__shared__ float Yr_a[N_okno];
	__shared__ float Zr_a[N_okno];
	__shared__ float Xl_a[N_okno];
	__shared__ float Yl_a[N_okno];
	__shared__ float Zl_a[N_okno];
	__shared__ float Xv_a[N_okno];
	__shared__ float Yv_a[N_okno];
	__shared__ float Zv_a[N_okno];
	__shared__ float Xn_a[N_okno];
	__shared__ float Yn_a[N_okno];
	__shared__ float Zn_a[N_okno];
	__shared__ float Xr_g[N_okno];
	__shared__ float Yr_g[N_okno];
	__shared__ float Zr_g[N_okno];
	__shared__ float Xl_g[N_okno];
	__shared__ float Yl_g[N_okno];
	__shared__ float Zl_g[N_okno];
	__shared__ float Xv_g[N_okno];
	__shared__ float Yv_g[N_okno];
	__shared__ float Zv_g[N_okno];
	__shared__ float Xn_g[N_okno];
	__shared__ float Yn_g[N_okno];
	__shared__ float Zn_g[N_okno];
	__shared__ float Rr[N_okno];
	__shared__ float Rl[N_okno];
	__shared__ float Rn[N_okno];
	__shared__ float Rv[N_okno];


	__shared__ vec right_[N_okno];
	__shared__ vec left_[N_okno];
	__shared__ vec up_[N_okno];
	__shared__ vec down_[N_okno];
	*/
	
/*	__shared__ vec right_l[N_okno];
	__shared__ vec left_r[N_okno];
	__shared__ vec up_n[N_okno];
	__shared__ vec down_v[N_okno];

*/
__global__ void Calc_ALL(uint32_t * in1,uint32_t * in2,uint32_t * in3,uint32_t * in4,uint32_t * in5,float*__x,float*__y,float*__z,float*__a,float*__b,float*__g,vec* left_right,vec* up_down, float* g_inuse,float*__tdis,float*__tdisup)
{//запускаеться для pf блока из 30 нитий.




	 float Rr_x;
	 float Rr_y;
	 float Rr_z;
	 float Rl_x;
	 float Rl_y;
	 float Rl_z;
	 float Rv_x;
	 float Rv_y;
	 float Rv_z;
	 float Rn_x;
	 float Rn_y;
	 float Rn_z;
	 float Xr_b;
	 float Yr_b;
	 float Zr_b;
	 float Xl_b;
	 float Yl_b;
	 float Zl_b;
	 float Xv_b;
	 float Yv_b;
	 float Zv_b;
	 float Xn_b;
	 float Yn_b;
	 float Zn_b;
	 float Xr_a;
	 float Yr_a;
	 float Zr_a;
	 float Xl_a;
	 float Yl_a;
	 float Zl_a;
	 float Xv_a;
	 float Yv_a;
	 float Zv_a;
	 float Xn_a;
	 float Yn_a;
	 float Zn_a;
	 float Xr_g;
	 float Yr_g;
	 float Zr_g;
	 float Xl_g;
	 float Yl_g;
	 float Zl_g;
	 float Xv_g;
	 float Yv_g;
	 float Zv_g;
	 float Xn_g;
	 float Yn_g;
	 float Zn_g;
	 float Rr;
	 float Rl;
	 float Rv;
	 float Rn;
float new_A1;
float new_A2;
float new_A3;
float new_A4;
	 
	uint32_t in_1;
	uint32_t in_2;
	uint32_t in_3;
	uint32_t in_4;
	uint32_t in_5;
	float x;
	float y;
	float z;
	float a;
	float b;
	float g;
	float exponental;
	float exponentar;
	float exponentan;
	float exponentav;
float new_expol;
float new_expor;
float new_expov;
float new_expon;
float new_rl;
float new_rr;
float new_rv;
float new_rn;
	float dUv;
	float dUn;
	float dUr;
	float dUl;
	float inuse;
	float inuse_z;
	float inuse_xy;
float eeexl;
float eeexr;
float eeexv;
float eeexn;
	vec right_l;
	vec left_r;
	vec up_n;
	vec down_v;
	
	vec right_;
	vec left_;
	vec up_;
	vec down_;
vec new_left;
vec new_right;
vec new_down;
vec new_up;
vec exponenta_r;
vec exponenta_l;
vec exponenta_v;
vec exponenta_n;


	int count;

	float sina;
	float cosa;
	float cosb;
	float sinb;
	float cosg;
	float sing;
	float cosw;
	float sinw;
	float zamena_a;
	float dispu;
	float dis;

/*
int tx=threadIdx.x;//0..12
int bx=blockIdx.x;//0..29
*/
/*так было при работе с 30 блоками по 13 нитей и оно работало на 295
int tx=threadIdx.x;
int bx=blockIdx.x+RAMKA[tx];
index=m*pf+j
m=0..N-1
j=0..12
*/

int tx=threadIdx.x%13;
int bx=2*blockIdx.x+threadIdx.x/13+RAMKA[tx];
float ffa=(float)((bx)%2);
float ffo=(float)((bx+1)%2);

/*
int tx=blockIdx.x; //0..12
int bx=threadIdx.x+RAMKA[tx]; //0..29
*/
//int shx=threadIdx.x;//индекс для обращения к массивами в шаред памяти.



int index=__mul24(2,tx+__mul24(bx,pf));
	int index_2=tx+__mul24(bx,pf);

	 in_1=in1[index_2];
	 in_2=in2[index_2];
	 in_3=in3[index_2];
	 in_4=in4[index_2];
	 in_5=in5[index_2];

	 x=__x[index_2];
	 y=__y[index_2];
	 z=__z[index_2];
	 a=__a[index_2];
	 b=__b[index_2];
	 g=__g[index_2];
dis=__tdis[index_2];
dispu=__tdisup[index_2];

cosw=cf_angle_[tx];
sinw=sf_angle_[tx];

	for( count =0 ;count <NfoPr;count++)//NfoPr
	{
sina=sinf(a);
sinb=sinf(b);
sing=sinf(g);
cosa=cosf(a);
cosb=cosf(b);
cosg=cosf(g);

	__b[index_2]=b;

	//right
	right_.x=(((-koren[0]*sinpi)*cosb+h_2*sinb)*cosg+koren[0]*cospi*sing)*cosw+(cosa*(-((-koren[0]*sinpi)*cosb+h_2*sinb)*sing+koren[0]*cospi*cosg)+sina*((-koren[0]*sinpi)*sinb-h_2*cosb))*sinw+x;
	right_.y=-(((-koren[0]*sinpi)*cosb+h_2*sinb)*cosg+koren[0]*cospi*sing)*sinw+(cosa*(-((-koren[0]*sinpi)*cosb+h_2*sinb)*sing+koren[0]*cospi*cosg)+sina*((-koren[0]*sinpi)*sinb-h_2*cosb))*cosw+y;
	right_.z=(-sina*(-((-koren[0]*sinpi)*cosb+h_2*sinb)*sing+koren[0]*cospi*cosg)+cosa*((-koren[0]*sinpi)*sinb-h_2*cosb))+z;
	left_right[index+1].x=right_.x;
	left_right[index+1].y=right_.y;
	left_right[index+1].z=right_.z;
	//down
	down_.x=((Rt*sinb)*cosg)*cosw+(cosa*(-(Rt*sinb)*sing)+sina*((-Rt)*cosb))*sinw+x;
	down_.y=-((Rt*sinb)*cosg)*sinw+(cosa*(-(Rt*sinb)*sing)+sina*(+(-Rt)*cosb))*cosw+y;
	down_.z=(-sina*(-(Rt*sinb)*sing)+cosa*((-Rt)*cosb))+z;
	up_down[index+1].x=down_.x;
	up_down[index+1].y=down_.y;
	up_down[index+1].z=down_.z;
	//left
	left_.x=(((-koren[0]*sinpi)*cosb-h_2*sinb)*cosg+(-koren[0]*cospi)*sing)*cosw+(cosa*(-((-koren[0]*sinpi)*cosb-h_2*sinb)*sing+(-koren[0]*cospi)*cosg)+sina*((-koren[0]*sinpi)*sinb+h_2*cosb))*sinw+x;
	left_.y=-(((-koren[0]*sinpi)*cosb-h_2*sinb)*cosg+(-koren[0]*cospi)*sing)*sinw+(cosa*(-((-koren[0]*sinpi)*cosb-h_2*sinb)*sing+(-koren[0]*cospi)*cosg)+sina*((-koren[0]*sinpi)*sinb+h_2*cosb))*cosw+y;
	left_.z=(-sina*(-((-koren[0]*sinpi)*cosb-h_2*sinb)*sing+(-koren[0]*cospi)*cosg)+cosa*((-koren[0]*sinpi)*sinb+h_2*cosb))+z;
	left_right[index].x=left_.x;
	left_right[index].y=left_.y;
	left_right[index].z=left_.z;
	//up

	up_.x=((-Rt*sinb)*cosg)*cosw+(cosa*(-(-Rt*sinb)*sing)+sina*(Rt*cosb))*sinw+x;
	up_.y=-((-Rt*sinb)*cosg)*sinw+(cosa*(-(-Rt*sinb)*sing)+sina*(Rt*cosb))*cosw+y;
	up_.z=(-sina*(-(Rt*sinb)*sing)+cosa*(Rt*cosb))+z;
	up_down[index].x=up_.x;
	up_down[index].y=up_.y;
	up_down[index].z=up_.z;





	///////////////////////////////////////



	__threadfence();

	down_v.x=up_down[down__[index_2]].x;
	down_v.y=up_down[down__[index_2]].y;
	down_v.z=up_down[down__[index_2]].z;
	right_l.x=left_right[right__[index_2]].x;
	right_l.y=left_right[right__[index_2]].y;
	right_l.z=left_right[right__[index_2]].z;
	left_r.x=left_right[left__[index_2]].x;
	left_r.y=left_right[left__[index_2]].y;
	left_r.z=left_right[left__[index_2]].z;
	up_n.x=up_down[up__[index_2]].x;
	up_n.y=up_down[up__[index_2]].y;
	up_n.z=up_down[up__[index_2]].z;

	Rr_x=right_.x- left_r.x;
	Rr_y=right_.y- left_r.y;
	Rr_z=right_.z- left_r.z;
	Rl_x=left_.x- right_l.x;
	Rl_z=left_.z- right_l.z;
	Rl_y=left_.y- right_l.y;
	Rv_x=up_.x - down_v.x;
	Rv_z=up_.z - down_v.z;
	Rv_y=up_.y- down_v.y;
	Rn_x=down_.x - up_n.x;
	Rn_z=down_.z - up_n.z;
	Rn_y=down_.y - up_n.y;

new_left.x=__x[rT__[index_2]]-x;
new_left.y=__y[rT__[index_2]]-y;
new_left.z=__z[rT__[index_2]]-z;
new_right.x=__x[lT__[index_2]]-x;
new_right.y=__y[lT__[index_2]]-y;
new_right.z=__z[lT__[index_2]]-z;
new_down.x=__x[uT__[index_2]]-x;
new_down.y=__y[uT__[index_2]]-y;
new_down.z=__z[uT__[index_2]]-z;
new_up.x=__x[dT__[index_2]]-x;
new_up.y=__y[dT__[index_2]]-y;
new_up.z=__z[dT__[index_2]]-z;




Rr=sqrtf( Rr_x*Rr_x+Rr_y*Rr_y+Rr_z*Rr_z);
Rl=sqrtf( Rl_x*Rl_x+Rl_y*Rl_y+Rl_z*Rl_z);
Rv=sqrtf( Rv_x*Rv_x+Rv_y*Rv_y+Rv_z*Rv_z);
Rn=sqrtf( Rn_x*Rn_x+Rn_y*Rn_y+Rn_z*Rn_z);
new_rl=sqrtf(new_left.x*new_left.x+new_left.y*new_left.y+new_left.z*new_left.z);
new_rr=sqrtf(new_right.x*new_right.x+new_right.y*new_right.y+new_right.z*new_right.z);
new_rv=sqrtf(new_up.x*new_up.x+new_up.y*new_up.y+new_up.z*new_up.z);
new_rn=sqrtf(new_down.x*new_down.x+new_down.y*new_down.y+new_down.z*new_down.z);

//proizvodnie dlia uglov


//b
	Xv_b=(((0*(-sinb)-Rt*cosb)*cosg)*cosw+(cosa*(-(0*(-sinb)-Rt*cosb)*sing)+sina*(0*cosb+Rt*(-sinb)))*sinw);
	Yv_b=-(((0*(-sinb)-Rt*cosb)*cosg)*sinw+(cosa*(-(0*(-sinb)-Rt*cosb)*sing)+sina*(0*cosb+Rt*(-sinb)))*cosw);
	Zv_b=((-sina*(-(0*(-sinb)+Rt*cosb)*sing)+cosa*(0*cosb+Rt*(-sinb))));


	Xr_b=(((-koren[0]*sinpi)*(-sinb)+h_2*cosb)*cosg)*cosw+(cosa*(-((-koren[0]*sinpi)*(-sinb)+h_2*cosb)*sing)+sina*((-koren[0]*sinpi)*cosb-h_2*(-sinb)))*sinw;
	Yr_b=-(((-koren[0]*sinpi)*(-sinb)+h_2*cosb)*cosg)*sinw+(cosa*(-((-koren[0]*sinpi)*(-sinb)+h_2*cosb)*sing)+sina*((-koren[0]*sinpi)*cosb-h_2*(-sinb)))*cosw;
	Zr_b=(-sina*(-((-koren[0]*sinpi)*(-sinb)+h_2*cosb)*sing)+cosa*((-koren[0]*sinpi)*cosb-h_2*(-sinb)));

	Xl_b=(((-koren[0]*sinpi)*(-sinb)-h_2*cosb)*cosg)*cosw+(cosa*(-((-koren[0]*sinpi)*(-sinb)-h_2*cosb)*sing)+sina*((-koren[0]*sinpi)*cosb+h_2*(-sinb)))*sinw;
	Yl_b=-(((-koren[0]*sinpi)*(-sinb)-h_2*cosb)*cosg)*sinw+(cosa*(-((-koren[0]*sinpi)*(-sinb)-h_2*cosb)*sing)+sina*((-koren[0]*sinpi)*cosb+h_2*(-sinb)))*cosw;
	Zl_b=(-sina*(-((-koren[0]*sinpi)*(-sinb)-h_2*cosb)*sing)+cosa*((-koren[0]*sinpi)*cosb+h_2*(-sinb)));


	Xn_b=((0*(-sinb)+Rt*cosb)*cosg)*cosw+(cosa*(-(0*(-sinb)+Rt*cosb)*sing)+sina*(0*cosb+(-Rt)*(-sinb)))*sinw;
	Yn_b=-((0*(-sinb)+Rt*cosb)*cosg)*sinw+(cosa*(-(0*(-sinb)+Rt*cosb)*sing)+sina*(0*cosb+(-Rt)*(-sinb)))*cosw;
	Zn_b=(-sina*(-(0*(-sinb)-Rt*cosb)*sing)+cosa*(0*cosb+(-Rt)*(-sinb)));

//a

    zamena_a=((-sina)*(-((-koren[0]*sinpi)*cosb+h_2*sinb)*sing+koren[0]*cospi*cosg)+cosa*((-koren[0]*sinpi)*sinb-h_2*cosb));
	Xr_a=zamena_a*sinw;
	Yr_a=zamena_a*cosw;
	Zr_a=(-cosa*(-((-koren[0]*sinpi)*cosb+h_2*sinb)*sing+koren[0]*cospi*cosg)+(-sina)*((-koren[0]*sinpi)*sinb-h_2*cosb));


Xn_a=((0*cosb+Rt*sinb)*cosg+0*sing)*cosw+((-sina)*(-(0*cosb+Rt*sinb)*sing+0*cosg)+cosa*(0*sinb+(-Rt)*cosb))*sinw;
Yn_a=-((0*cosb+Rt*sinb)*cosg+0*sing)*sinw+((-sina)*(-(0*cosb+Rt*sinb)*sing+0*cosg)+cosa*(0*sinb+(-Rt)*cosb))*cosw;
Zn_a=(-cosa*(-(0*cosb+Rt*sinb)*sing+0*cosg)+(-sina)*(0*sinb+(-Rt)*cosb));

	 zamena_a=((-sina)*(-((-koren[0]*sinpi)*cosb-h_2*sinb)*sing+(-koren[0]*cospi)*cosg)+cosa*((-koren[0]*sinpi)*sinb+h_2*cosb));
	Xl_a=zamena_a*sinw;
	Yl_a=zamena_a*cosw;
	Zl_a=(-cosa*(-((-koren[0]*sinpi)*cosb-h_2*sinb)*sing+(-koren[0]*cospi)*cosg)+(-sina)*((-koren[0]*sinpi)*sinb+h_2*cosb));

		 zamena_a=((-sina)*(-(0*cosb-Rt*sinb)*sing+0*cosg)+cosa*(0*sinb+Rt*cosb));
	Xv_a= zamena_a*sinw;
	Yv_a= zamena_a*cosw;
	Zv_a=(-cosa*(-(0*cosb+Rt*sinb)*sing+0*cosg)+(-sina)*(0*sinb+Rt*cosb));


//g

	Xr_g=(((-koren[0]*sinpi)*cosb+h_2*sinb)*(-sing)+koren[0]*cospi*cosg)*cosw+(cosa*(-((-koren[0]*sinpi)*cosb+h_2*sinb)*cosg+koren[0]*cospi*(-sing)))*sinw;
	Yr_g=-(((-koren[0]*sinpi)*cosb+h_2*sinb)*(-sing)+koren[0]*cospi*cosg)*sinw+(cosa*(-((-koren[0]*sinpi)*cosb+h_2*sinb)*cosg+koren[0]*cospi*(-sing)))*cosw;
	Zr_g=(-sina*(-((-koren[0]*sinpi)*cosb+h_2*sinb)*cosg+koren[0]*cospi*(-sing)));


Xn_g=((0*cosb+Rt*sinb)*(-sing)+0*cosg)*cosw+(cosa*(-(0*cosb+Rt*sinb)*cosg+0*(-sing)))*sinw;
Yn_g=-((0*cosb+Rt*sinb)*(-sing)+0*cosg)*sinw+(cosa*(-(0*cosb+Rt*sinb)*cosg+0*(-sing)))*cosw;
Zn_g=(-sina*(-(0*cosb+Rt*sinb)*cosg+0*(-sing)));


	Xl_g=(((-koren[0]*sinpi)*cosb-h_2*sinb)*(-sing)+(-koren[0]*cospi)*cosg)*cosw+(cosa*(-((-koren[0]*sinpi)*cosb-h_2*sinb)*cosg+(-koren[0]*cospi)*(-sing)))*sinw;
	Yl_g=-(((-koren[0]*sinpi)*cosb-h_2*sinb)*(-sing)+(-koren[0]*cospi)*cosg)*sinw+(cosa*(-((-koren[0]*sinpi)*cosb-h_2*sinb)*cosg+(-koren[0]*cospi)*(-sing)))*cosw;
	Zl_g=(-sina*(-((-koren[0]*sinpi)*cosb-h_2*sinb)*cosg+(-koren[0]*cospi)*(-sing)));


	Xv_g=((0*cosb-Rt*sinb)*(-sing)+0*cosg)*cosw+(cosa*(-(0*cosb-Rt*sinb)*cosg+0*(-sing)))*sinw;
	Yv_g=-((0*cosb-Rt*sinb)*(-sing)+0*cosg)*sinw+(cosa*(-(0*cosb-Rt*sinb)*cosg+0*(-sing)))*cosw;
	Zv_g=(-sina*(-(0*cosb+Rt*sinb)*cosg+0*(-sing)));


/// POTENCIALI POTENCIALI POTENCIALI POTENCIALI POTENCIALI POTENCIALI POTENCIALI POTENCIALI
	//g_inuse[rT__[index_2]]*

inuse=g_inuse[index_2];
if (((Rn> r0_4)&&(Rr> r0_4))||((Rn> r0_4)&&(Rl> r0_4)))
{
	inuse=0.0f;
};
g_inuse[index_2]=inuse;
if (Rv> r0_4)
{
	inuse_z=0.0f;
};
if ((Rl> r0_4)||(Rr> r0_4))
{
	inuse_xy=0.0f;
};

__threadfence();
new_A1=((new_rl<w)?new_AA*A:0.0f);
new_A2=((new_rr<w)?new_AA*A:0.0f);
new_A3=((new_rv<w)?new_AA*K:0.0f);
new_A4=((new_rn<w)?new_AA*K:0.0f);
new_expol=2*new_A1*(new_rl-w)*expf(-(new_rl-w)*_wo)+new_A1*(new_rl-w)*(new_rl-w)*expf(-(new_rl-w)*_wo)*(-_wo);
new_expor=2*new_A2*(new_rr-w)*expf(-(new_rr-w)*_wo)+new_A2*(new_rr-w)*(new_rr-w)*expf(-(new_rr-w)*_wo)*(-_wo);
new_expov=2*new_A3*(new_rv-w)*expf(-(new_rv-w)*_wo)+new_A3*(new_rv-w)*(new_rv-w)*expf(-(new_rv-w)*_wo)*(-_wo);
new_expon=2*new_A4*(new_rn-w)*expf(-(new_rn-w)*_wo)+new_A4*(new_rn-w)*(new_rn-w)*expf(-(new_rn-w)*_wo)*(-_wo);
eeexl=((new_rl<1.0f)?0.0f:new_expol/new_rl);
eeexr=((new_rr<1.0f)?0.0f:new_expor/new_rr);
eeexv=((new_rv<1.0f)?0.0f:new_expov/new_rv);
eeexn=((new_rn<1.0f)?0.0f:new_expon/new_rn);
exponenta_l.x=eeexl*new_left.x*(-1.0f);
exponenta_l.y=eeexl*new_left.y*(-1.0f);
exponenta_l.z=eeexl*new_left.z*(-1.0f);

exponenta_r.x=eeexr*new_right.x*(-1.0f);
exponenta_r.y=eeexr*new_right.y*(-1.0f);
exponenta_r.z=eeexr*new_right.z*(-1.0f);

exponenta_v.x=g_inuse[dT__[index_2]]*(ffa*eeexv*new_up.x)*(-1.0f);
exponenta_v.y=g_inuse[dT__[index_2]]*(ffa*eeexv*new_up.y)*(-1.0f);
exponenta_v.z=g_inuse[dT__[index_2]]*(ffa*eeexv*new_up.z)*(-1.0f);

exponenta_n.x=g_inuse[uT__[index_2]]*(ffo*eeexn*new_down.x)*(-1.0f);
exponenta_n.y=g_inuse[uT__[index_2]]*(ffo*eeexn*new_down.y)*(-1.0f);
exponenta_n.z=g_inuse[uT__[index_2]]*(ffo*eeexn*new_down.z)*(-1.0f);




exponental=g_inuse[rT__[index_2]]*(A*expf(-Rl/Ro)-(1.0f/Ro)*expf(-Rl/Ro)*0.5f*A*Rl+per_ex*A*expf(-Rl*Rl/(v_ex*Ro))*(1.0f/(v_ex*Ro)));
exponentar=g_inuse[lT__[index_2]]*(A*expf(-Rr/Ro)-(1.0f/Ro)*expf(-Rr/Ro)*0.5f*A*Rr+per_ex*A*expf(-Rr*Rr/(v_ex*Ro))*(1.0f/(v_ex*Ro)));
exponentav=g_inuse[dT__[index_2]]*(ffa*(K*expf(-Rv/Ro)-(1.0f/Ro)*expf(-Rv/Ro)*0.5f*K*Rv+per_ex*K*expf(-Rv*Rv/(v_ex*Ro))*(1.0f/(v_ex*Ro)))+ffo*0.07f*K );
exponentan=g_inuse[uT__[index_2]]*(ffo*(K*expf(-Rn/Ro)-(1.0f/Ro)*expf(-Rn/Ro)*0.5f*K*Rn+per_ex*K*expf(-Rn*Rn/(v_ex*Ro))*(1.0f/(v_ex*Ro)))+ffa*0.07f*K );


x=x-inuse*Gamma*DT*(Rv_x*(exponentav)+Rn_x*(exponentan)+Rl_x*(exponental)+Rr_x*(exponentar)+exponenta_l.x+exponenta_r.x+exponenta_v.x+exponenta_n.x)+inuse*koeff[0]*Random_New(&in_1,&in_2,&in_3,&in_4,&in_5);//Vspomni O RASSTOjanii ego tut ne nado
y=y-inuse*Gamma*DT*(Rv_y*(exponentav)+Rn_y*(exponentan)+Rl_y*(exponental)+Rr_y*(exponentar)+exponenta_l.y+exponenta_r.y+exponenta_v.y+exponenta_n.y)+inuse*koeff[0]*Random_New(&in_1,&in_2,&in_3,&in_4,&in_5);//Vspomni O RASSTOjanii ego tut ne nado
z=z-inuse*Gamma*DT*(Rv_z*(exponentav)+Rn_z*(exponentan)+Rl_z*(exponental)+Rr_z*(exponentar)+exponenta_l.z+exponenta_r.z+exponenta_v.z+exponenta_n.z)+inuse*koeff[0]*Random_New(&in_1,&in_2,&in_3,&in_4,&in_5);//Vspomni O RASSTOjanii ego tut ne nado
	__x[index_2]=x;
	__y[index_2]=y;
	__z[index_2]=z;

dispu=((dispu==0.0f)&&(Rv>r04)&&(inuse!=0.0f))?(1.0f*(float)count):dispu;
dis=((dis==0.0f)&&((Rl>r12)||(Rr>r12)))?(1.0f*(float)count):dis;
		
	




// ANGLES////pomeniala bx na tx 4 okt v 1 00

if (g_inuse[uT__[index_2]]==0.0f)
{g_inuse[index_2]=0.0f;
};
dUv=exponentav*(Rv_x*Xv_b+Rv_y*Yv_b+Rv_z*Zv_b);
dUn=exponentan*(Rn_x*Xn_b+Rn_y*Yn_b+Rn_z*Zn_b);
dUr=exponentar*(Rr_x*Xr_b+Rr_y*Yr_b+Rr_z*Zr_b);
dUl=exponental*(Rl_x*Xl_b+Rl_y*Yl_b+Rl_z*Zl_b);
b=b-K_2b*(b-__b[uT__[index_2]]+Bo)*DT*Gammay+g_inuse[dT__[index_2]]*K_2b*(__b[dT__[index_2]]-b+Bo)*DT*Gammay*isup[bx]-(dUv+dUn+dUr+dUl)*DT*Gammay+istreh*koeff2[0]*Random_New(&in_1,&in_2,&in_3,&in_4,&in_5);

dUv=exponentav*(Rv_x*Xv_a+Rv_y*Yv_a+Rv_z*Zv_a);
dUn=exponentan*(Rn_x*Xn_a+Rn_y*Yn_a+Rn_z*Zn_a);
dUr=exponentar*(Rr_x*Xr_a+Rr_y*Yr_a+Rr_z*Zr_a);
dUl=exponental*(Rl_x*Xl_a+Rl_y*Yl_a+Rl_z*Zl_a);
a=a-K_2a*(a-Ao)*DT*Gammay-(dUv+dUn+dUr+dUl)*DT*Gammay+istreh*koeff2[0]*Random_New(&in_1,&in_2,&in_3,&in_4,&in_5);

dUv=exponentav*(Rv_x*Xv_g+Rv_y*Yv_g+Rv_z*Zv_g);
dUn=exponentan*(Rn_x*Xn_g+Rn_y*Yn_g+Rn_z*Zn_g);
dUr=exponentar*(Rr_x*Xr_g+Rr_y*Yr_g+Rr_z*Zr_g);
dUl=exponental*(Rl_x*Xl_g+Rl_y*Yl_g+Rl_z*Zl_g);
g=g-K_2g*(g-Go)*DT*Gammay-(dUv+dUn+dUr+dUl)*DT*Gammay+istreh*koeff2[0]*Random_New(&in_1,&in_2,&in_3,&in_4,&in_5);


		}
	__x[index_2]=x;
	__y[index_2]=y;
	__z[index_2]=z;
	__a[index_2]=a;
	__b[index_2]=b;
	__g[index_2]=g;
	in1[index_2]=in_1;
	in2[index_2]=in_2;
	in3[index_2]=in_3;
	in4[index_2]=in_4;
	in5[index_2]=in_5;
	__tdis[index_2]=dis;
	__tdisup[index_2]=dispu;
__threadfence();

}



////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
int
main( int argc, char** argv)
{

	float _time[1];
	_time[0]=0.0f;
	//SET DEVICE
	cudaSetDevice(1);
	//SET DEVICE
	float * cu_x;
	float * cu_y;
	float * cu_z;
	float * cu_a;
	float * cu_b;
	float * cu_g;
	float * cu_t;

	bool spfind =false;
	bool spfirstfind =false;
	uint curpos6=N;
	uint curpos13=N-8;
	uint  SPEED__[N];
	float ZMAX=0.0f;

	float * cu_RandX=NULL;

	uint32_t * cu_in1X=NULL;
	uint32_t * cu_in2X=NULL;
	uint32_t * cu_in3X=NULL;
	uint32_t * cu_in4X=NULL;
	uint32_t * cu_in5X=NULL;
	float * g_inuse=NULL;
	//int N_elements=pf*N;

	float *cu_tdis=NULL;
	float host_tdis[pf*N];
	float *cu_tdisup=NULL;
	float host_tdisup[pf*N];
	vec * cu_ud=NULL;//координаты центров взаимодействия (четное - верхний не четное нижний) шарика
	vec * cu_lr=NULL;//координаты центра  взаимодействия (четное - левый не четное правый) шарика

	vec host_lr[2*pf*N];//координаты центра шарика
	vec host_ud[2*pf*N];//координаты центра шарика
	float  host_x[pf*N];
	float  host_y[pf*N];
	float  host_z[pf*N];
	float  host_a[pf*N];
	float  host_b[pf*N];
	float  host_g[pf*N];
	bool  done__[pf*N];
	bool  done2__[pf*N];
	float  host_t[pf*N];
	float  host_xRand[pf*N];
	float  host_inuse[pf*N];
	int  host_RAMKA[pf];
	float zahod[pf*N];
	int * cu_rand_seed;
	int host_rand_seed[pf*N];
	int green;
	int blue;
	cudaMalloc ( (void **)&cu_rand_seed,(size_t)pf*sizeof(int)*N);
//	cuMemAllocPitch(&cu_rand_seed,&pitch,sizeof(float)*8,30,16);

	cudaMalloc ( (void **)&cu_x,(size_t)pf*sizeof(float)*N);
	//cuMemAllocPitch(&cu_x,&pitch,sizeof(float)*8,30,16);
	cudaMalloc ( (void **)&cu_y,(size_t)pf*sizeof(float)*N);
	cudaMalloc ( (void **)&cu_z,(size_t)pf*sizeof(float)*N);
	cudaMalloc ( (void **)&cu_t,(size_t)pf*sizeof(float)*N);
	cudaMalloc ( (void **)&cu_a,(size_t)pf*sizeof(float)*N);
	cudaMalloc ( (void **)&cu_b,(size_t)pf*sizeof(float)*N);
	cudaMalloc ( (void **)&cu_g,(size_t)pf*sizeof(float)*N);


	cudaMalloc ( (void **)&g_inuse,(size_t)pf*sizeof(float)*N);
	cudaMalloc ( (void **)&cu_tdis,(size_t)pf*sizeof(float)*N);
	cudaMalloc ( (void **)&cu_tdisup,(size_t)pf*sizeof(float)*N);
	cudaMalloc ( (void **)&cu_RandX,(size_t)pf*sizeof(float)*N);


	cudaMalloc ( (void **)&cu_lr,(size_t)pf*sizeof(vec)*N*2);
	cudaMalloc ( (void **)&cu_ud,(size_t)pf*sizeof(vec)*N*2);

	cudaMalloc ( (void **)&cu_in1X,(size_t)pf*sizeof(uint32_t)*N);
	cudaMalloc ( (void **)&cu_in2X,(size_t)pf*sizeof(uint32_t)*N);
	cudaMalloc ( (void **)&cu_in3X,(size_t)pf*sizeof(uint32_t)*N);
	cudaMalloc ( (void **)&cu_in4X,(size_t)pf*sizeof(uint32_t)*N);
	cudaMalloc ( (void **)&cu_in5X,(size_t)pf*sizeof(uint32_t)*N);


	for(int i=0;i<N*pf;i++)
	{
		host_inuse[i]=1.0f;
		host_tdis[i]=0.0f;
		host_tdisup[i]=0.0f;
		done__[i]=true;
		done2__[i]=true;
	}

	cudaThreadSynchronize();
	cudaMemcpy  ( g_inuse,host_inuse, (size_t)pf*sizeof(float)*N, cudaMemcpyHostToDevice );
	cudaMemcpy  ( cu_tdis,host_tdis, (size_t)pf*sizeof(float)*N, cudaMemcpyHostToDevice );
	cudaMemcpy  ( cu_tdisup,host_tdisup, (size_t)pf*sizeof(float)*N, cudaMemcpyHostToDevice );


	dim3 threads_alfa ( N );
	dim3 blocks_alfa  ( pf);
	dim3 bl_N_4 ( 30 );
	dim3 bl_26 ( 15 );
	dim3 thr_26 ( 2*pf );
	//dim3 thr_8 ( 30 );
	//dim3 bl_8 ( 8 );

	time_t seconds;
	time_t second0;
	second0 = time (NULL);
	cudaEvent_t start, stop;
	float gpuTime = 0.0f;
	cudaEventCreate ( &start );
	cudaEventCreate ( &stop );
	SetConstMem() ;
	cudaThreadSynchronize();
	CoordXYZABGDefaulteKernel<<<blocks_alfa,threads_alfa>>>( cu_x,cu_y,cu_z,cu_a,cu_b,cu_g) ;
	cudaThreadSynchronize();
	srand ((unsigned int) time(NULL) );
//Побготовка рандома
	for (int i=0;i<pf*N;i++)
	{
		host_rand_seed[i]=rand();
	}

	cudaMemcpy  ( cu_rand_seed,host_rand_seed, (size_t)pf*sizeof(int)*N, cudaMemcpyHostToDevice );
	InitRandom<<<blocks_alfa,threads_alfa>>>(cu_rand_seed,cu_in1X,cu_in2X,cu_in3X,cu_in4X,cu_in5X);
	cudaThreadSynchronize();

	FILE *file;
	FILE *file2;
	FILE *file3;
	FILE *file4;
	FILE *file5;
	FILE *file6;
	FILE *file8;
	
	char buf[50];//=(char*)malloc(50*sizeof(char));
	time (&second0);
	
//	sprintf(buf,"%04d-%02d-%02d_%02d%02d%02d",tblock->tm_year+1900,tblock->tm_mon+1,tblock->tm_mday+1,tblock->tm_hour,tblock->tm_min,tblock->tm_sec);
/*
	
	char buf[50];//=(char*)malloc(50*sizeof(char));
	sprintf_s(buf,50,"%04d-%02d-%02d_%02d%02d%02d",tblock.tm_year+1900,tblock.tm_mon,tblock.tm_mday,tblock.tm_hour,tblock.tm_min,tblock.tm_sec);*/


	char file_name[50];//=(char*)malloc(50*sizeof(char));
	char file_name2[50];//=(char*)malloc(50*sizeof(char));
	char file_name3[50];//=(char*)malloc(50*sizeof(char));
	char file_name4[50];//=(char*)malloc(50*sizeof(char));
	char file_name5[50];//=(char*)malloc(50*sizeof(char));
	char file_name6[50];//=(char*)malloc(50*sizeof(char));
	char file_name8[50];//=(char*)malloc(50*sizeof(char));
	

#ifdef _WIN32
	struct tm tblock;
	localtime_s(&tblock,&second0);
	sprintf_s(buf,50,"%04d-%02d-%02d_%02d%02d%02d",tblock.tm_year+1900,tblock.tm_mon,tblock.tm_mday,tblock.tm_hour,tblock.tm_min,tblock.tm_sec);

	printf("Program 's begun work at %s\n",buf);
	strcpy_s(file_name,_countof(file_name),"!file");
	strcat_s(file_name,_countof(file_name),buf);
	strcat_s(file_name,_countof(file_name),".txt");
	strcpy_s(file_name2,_countof(file_name2),"!pot");
	strcat_s(file_name2,_countof(file_name2),buf);
	strcat_s(file_name2,_countof(file_name2),".txt");
	strcpy_s(file_name3,_countof(file_name3),"!param");
	strcat_s(file_name3,_countof(file_name3),buf);
	strcat_s(file_name3,_countof(file_name3),".txt");
	strcpy_s(file_name4,_countof(file_name4),"!timmingup");
	strcat_s(file_name4,_countof(file_name4),buf);
	strcat_s(file_name4,_countof(file_name4),".txt");
	strcpy_s(file_name5,_countof(file_name5),"!timing");
	strcat_s(file_name5,_countof(file_name5),buf);
	strcat_s(file_name5,_countof(file_name5),".txt");
	strcpy_s(file_name6,_countof(file_name6),"!speed");
	strcat_s(file_name6,_countof(file_name6),buf);
	strcat_s(file_name6,_countof(file_name6),".txt");
	
	strcpy_s(file_name8,_countof(file_name8),"!dinum_");
	strcat_s(file_name8,_countof(file_name8),buf);
	strcat_s(file_name8,_countof(file_name8),".txt");

	fopen_s(&file, file_name, "w" );
	fopen_s(&file2, file_name2, "w" );
	fopen_s(&file3, file_name3, "w" );
	fopen_s(&file4, file_name4, "w" );
	fopen_s(&file5, file_name5, "w" );
	fopen_s(&file6, file_name6, "w" );
	fopen_s(&file8, file_name8, "w" );
	
#elif defined(linux)
	struct tm * tblock;
	tblock = localtime(&second0);
	sprintf(buf,"%04d-%02d-%02d_%02d%02d%02d",tblock->tm_year+1900,tblock->tm_mon+1,tblock->tm_mday+1,tblock->tm_hour,tblock->tm_min,tblock->tm_sec);
	printf("Program 's begun work at %s\n",buf);
 	strcpy(file_name,"!file");
	strcat(file_name,buf);
	strcat(file_name,".txt");
	strcpy(file_name2,"!pot");
	strcat(file_name2,buf);
	strcat(file_name2,".txt");
	strcpy(file_name3,"!param");
	strcat(file_name3,buf);
	strcat(file_name3,".txt");
	strcpy(file_name4,"!timmingup");
	strcat(file_name4,buf);
	strcat(file_name4,".txt");
	strcpy(file_name5,"!timing");
	strcat(file_name5,buf);
	strcat(file_name5,".txt");
	strcpy(file_name6,"!speed");
	strcat(file_name6,buf);
	strcat(file_name6,".txt");
	strcpy(file_name8,"!dinum_");
	strcat(file_name8,buf);
	strcat(file_name8,".txt");


	file=	fopen( file_name, "w" );
	file2=	fopen( file_name2, "w" );
	file3=	fopen( file_name3, "w" );
	file4=	fopen( file_name4, "w" );
	file5=	fopen( file_name5, "w" );
	file6=	fopen( file_name6, "w" );
	file8=	fopen( file_name8, "w" );
#else
    #error "What's your operating system?"
#endif
	


for(int i=0;i<N;i++)
	{
	
		SPEED__[i]=13;
	}

	/*file3 = fopen( file_name3, "w" );
	file2 = fopen(file_name2, "w" ); */
	if(file==NULL || file2==NULL )
	{
		printf("File isn`t open!");
		getchar();
		return 1;
	}
	/*
	fprintf(file3,"#dt=%e\n",dt);
	fprintf(file3,"#T=%e\n",T);
	fprintf(file3,"#B=%e\n",B);
	fprintf(file3,"#k=%e\n",k);
	fprintf(file3,"#r1=%e latera\n",r1);
	fprintf(file3,"#r0=%e vertikal\n",r0);
	fprintf(file3,"#A=%e\n",A);
	fprintf(file3,"#g1=%e\n",g1);
	fprintf(file3,"#g2=%e\n",g2);
	fprintf(file3,"#N=%d\n",N);
	fprintf(file3,"#NpFrame=%d\n",NfoPr);
*/
	
		fprintf(file3,"#dt=%e\n",DT);
		fprintf(file3,"#N=%d\n",N);
		fprintf(file3,"#T=%e\n",T);
		fprintf(file3,"#KT=%e\n",(Kbol*T));
		fprintf(file3,"#Ao=%e\n",Ao);
		fprintf(file3,"#Bo=%e\n",Bo);
		fprintf(file3,"#Go=%e\n",Go);
		fprintf(file3,"#Ka=%e\n",Ka);
		fprintf(file3,"#Kb=%e\n",Kb);
		fprintf(file3,"#Kg=%e\n",Kg);
		fprintf(file3,"#K longitudal=%e\n",K);
		fprintf(file3,"#K=%e /K*305.16\n",K/(Kbol*305.16));
		fprintf(file3,"#A lateral=%e\n",A);
		fprintf(file3,"#A=%e /K*305.16\n",A/(Kbol*305.16));
		fprintf(file3,"#Gamma=%e\n",Gamma);
		fprintf(file3,"#Gammay=%e\n",Gammay);
		fprintf(file3,"#N_OUT_ALL=%d\n",N_OUT_ALL);
		fprintf(file3,"#NpFrame=%d\n",NfoPr);

		fprintf(file3,"#w =%e\n",w);
		fprintf(file3,"#_wo=%e\n",_wo);
		fprintf(file3,"#new_AA=%e\n",new_AA);
		fprintf(file3,"#per_ex=%e\n",per_ex);
		fprintf(file3,"#v_ex=%e\n",v_ex);
		

	fclose(file3);
	seconds = time (NULL);
	printf("Time step #: %d , seconds from begin: %ld\n",0,(seconds-second0));
	DoRandom_new<<<blocks_alfa,threads_alfa>>>(cu_in1X,cu_in2X,cu_in3X,cu_in4X,cu_in5X,cu_RandX);
	cudaThreadSynchronize();
	NewKernel_KOOR<<<blocks_alfa,threads_alfa>>>( cu_x,cu_y,cu_z,cu_a,cu_b,cu_g,cu_lr,cu_ud) ;
	cudaThreadSynchronize();
	cudaMemcpy  ( host_xRand,cu_RandX,  sizeof(float)*N*pf, cudaMemcpyDeviceToHost );
	cudaThreadSynchronize();
	/*
for(int i=0; i<N*pf;i++)
{   zahod[i]=0.0f;
	fprintf(file4,"%e\n",host_xRand[i]);

}
fflush(file4);
*/
	cudaMemcpy  ( host_lr,cu_lr, sizeof(vec)*2*N*pf, cudaMemcpyDeviceToHost );
	cudaMemcpy  ( host_ud,cu_ud, sizeof(vec)*2*N*pf, cudaMemcpyDeviceToHost );
	cudaMemcpy  ( host_x,cu_x, sizeof(float)*N*pf, cudaMemcpyDeviceToHost );
	cudaMemcpy  ( host_y,cu_y, sizeof(float)*N*pf, cudaMemcpyDeviceToHost );
	cudaMemcpy  ( host_z,cu_z, sizeof(float)*N*pf, cudaMemcpyDeviceToHost );
	cudaMemcpy  ( host_t,cu_t, sizeof(float)*N*pf, cudaMemcpyDeviceToHost );

	cudaThreadSynchronize();
	printf("time spent executing by the GPU: %e millseconds, step N=%d \n", gpuTime,0 );
	fprintf(file,"#Frame\n");
	fprintf(file,"#MT\n");
	int index=0;
	for(int j =0;j<13;j++)
	{ host_RAMKA[j]= N-N_okno;
		for(int m=0;m<N;m++)
		{
			index=m*pf+j;
			if(host_inuse[index]==1.0f)
			{
			fprintf(file,"%f;%f;%f;%f;128;%d;%d;\n",host_x[index],host_y[index],host_z[index],r,m,j*10);
			fprintf(file,"%f;%f;%f;%f;%d;%d;%d;\n",host_lr[2*index+1].x,host_lr[2*index+1].y,host_lr[2*index+1].z,r/10,0,255,0);
			fprintf(file,"%f;%f;%f;%f;%d;%d;%d;\n",host_lr[2*(index)].x,host_lr[2*(index)].y,host_lr[2*(index)].z,r/10,255,0,0);
			fprintf(file,"%f;%f;%f;%f;%d;%d;%d;\n",host_ud[2*(index)+1].x,host_ud[2*(index)+1].y,host_ud[2*(index)+1].z,r/10,0,0,255);
			fprintf(file,"%f;%f;%f;%f;%d;%d;%d;\n",host_ud[2*(index)].x,host_ud[2*(index)].y,host_ud[2*(index)].z,r/10,0,255,255);
			};
		}
	}


	fflush(file);
	cudaMemcpyToSymbol(RAMKA, host_RAMKA, sizeof(host_RAMKA));
	seconds = time (NULL);
	printf("Time step #: %d , seconds from begin: %ld\n",0,(seconds-second0));
//	NewKernel_KOOR<<<blocks_alfa,threads_alfa>>>( cu_x,cu_y,cu_z,cu_a,cu_b,cu_g,cu_lr,cu_ud,pitch,pitch2) ;

	for( long unsigned  i=1;i<N_OUT_ALL;i++ )
	{		gpuTime = 0.0f;
			ZMAX=0.0f;
			_time[0]=(float)NfoPr*i*(float)DT;
			cudaEventRecord ( start, 0 );
			cudaThreadSynchronize();
			Calc_ALL<<<bl_26, thr_26 >>>(cu_in1X,cu_in2X,cu_in3X,cu_in4X,cu_in5X,cu_x,cu_y,cu_z,cu_a,cu_b,cu_g,cu_lr,cu_ud, g_inuse,cu_tdis,cu_tdisup);
			cudaThreadSynchronize();

			cudaEventRecord ( stop, 0 );
			cudaEventSynchronize ( stop );
			cudaEventElapsedTime ( &gpuTime, start, stop );
			printf("time spent Memory copy from GPU : %e millseconds, step N=%d \n", gpuTime,NfoPr*i );
			cudaMemcpy  ( host_x,cu_x, sizeof(float)*N*pf, cudaMemcpyDeviceToHost );
			if(cudaMemcpy  ( host_y,cu_y, sizeof(float)*N*pf, cudaMemcpyDeviceToHost )!=cudaSuccess)
			throw printf("CUDA failed to upload cu_y.");
			if(cudaMemcpy  ( host_z,cu_z, sizeof(float)*N*pf, cudaMemcpyDeviceToHost )!=cudaSuccess)
			throw printf("CUDA failed to upload cu_z.");
			cudaMemcpy  ( host_lr,cu_lr, sizeof(vec)*2*N*pf, cudaMemcpyDeviceToHost );
			cudaMemcpy  ( host_ud,cu_ud, sizeof(vec)*2*N*pf, cudaMemcpyDeviceToHost );
			cudaMemcpy  ( host_a,cu_a, sizeof(float)*N*pf, cudaMemcpyDeviceToHost );
			cudaMemcpy  ( host_b,cu_b, sizeof(float)*N*pf, cudaMemcpyDeviceToHost );
			cudaMemcpy  ( host_g,cu_g, sizeof(float)*N*pf, cudaMemcpyDeviceToHost );
			cudaMemcpy  ( host_tdis,cu_tdis, sizeof(float)*N*pf, cudaMemcpyDeviceToHost );
			cudaMemcpy  ( host_tdisup,cu_tdisup, sizeof(float)*N*pf, cudaMemcpyDeviceToHost );
			cudaMemcpy  ( host_inuse,g_inuse, sizeof(float)*N*pf, cudaMemcpyDeviceToHost );
			fprintf(file,"#Frame\n");
			fprintf(file,"#MT\n");
			fprintf(file2,"\t\t;;;;#Frame, time=%f\n",_time[0]);
			fprintf(file5,"\n");
			cudaThreadSynchronize();
			for(int j =0;j<13;j++)
			{
				for(int m=0;m<N;m++)
				{
				index=m*pf+j;
				if(host_inuse[index]==1.0f)
				{	
				if(	host_z[index]>ZMAX)
						{
						ZMAX=host_z[index];
						}
				green=255-100*(m % 2);
				blue=30;
				fprintf(file,"%f;%f;%f;%f;30;%d;%d;\n",host_x[index],host_y[index],host_z[index],r,green,blue);
				fprintf(file2,"%f;%f;%f; %d; %d;\n",host_a[index],host_b[index],host_g[index],m,j);

				}
				if(host_inuse[index]==0.0f)
				{SPEED__[m]-=1;
				//fprintf(file,"%f;%f;%f;%f;128;%d;%d;\n",host_x[index],host_y[index],host_z[index],r,m,j*10);
				};
				if((host_tdis[index]!=0.0f)&&(done__[index]))
				{
				done__[index]=false;
				fprintf(file5,"%d;%d;%d;%e\n",m,j,index,_time[0]+DT*host_tdis[index]);

				};
				if((host_tdisup[index]!=0.0f)&&(done2__[index]))
				{
				done2__[index]=false;
				fprintf(file4,"%d;%d;%d;%e\n",m,j,index,_time[0]+DT*host_tdisup[index]);
						for(int tes=m+1;tes<N;tes++)
						{
							if(host_tdisup[tes*pf+j]!=0.0f)
							{
								fprintf(file8,"%d;%d;%d;%e;%d;%d\n",m,j,index,_time[0]+DT*host_tdisup[index],tes,tes-m);
								//printf("N_dis UP-DOWNupto:  m=%d;%d;%d;tes=%d;%d\n",m,j,index,tes,tes-m);
								done2__[index]=true;
								break;
							}
						}
						if(!done2__[index])
						{
							fprintf(file8,"%d;%d;%d;%e;%d;%d\n",m,j,index,_time[0]+DT*host_tdisup[index],m,N-m);
							//printf("N_dis UP-DOWN_upall:  %d;%d;%d;%d\n",m,j,index,N-m);
						}
						done2__[index]=false;
				};
				
				
				
				
				/*
				fprintf(file,"%f;%f;%f;%f;%d;%d;%d;\n",host_lr[2*index+1].x,host_lr[2*index+1].y,host_lr[2*index+1].z,r/10,0,255,0);
				fprintf(file,"%f;%f;%f;%f;%d;%d;%d;\n",host_lr[2*(index)].x,host_lr[2*(index)].y,host_lr[2*(index)].z,r/10,255,0,0);
				fprintf(file,"%f;%f;%f;%f;%d;%d;%d;\n",host_ud[2*(index)+1].x,host_ud[2*(index)+1].y,host_ud[2*(index)+1].z,r/10,0,0,255);
				fprintf(file,"%f;%f;%f;%f;%d;%d;%d;\n",host_ud[2*(index)].x,host_ud[2*(index)].y,host_ud[2*(index)].z,r/10,0,255,255);	  */
				if ((host_inuse[index]==0.0f)&&(host_RAMKA[j]>4)&&(zahod[index]==0.0f))
				{
					for(int qum=m;qum<N;qum++)
					{zahod[(qum*pf+j)]=1.0f;
						host_inuse[(qum*pf+j)]=0.0f;
					};
					if(m>(N_okno+4))
					{	zahod[index]=1.0f;
						host_RAMKA[j]=m-N_okno;
					}


				};
				}

			}
			for(int m=0;m<N;m++)
			{
				if((SPEED__[m]<=6)&&(!spfind))
				{
					curpos6=m;
					spfind=true;
					
				}
				if((SPEED__[m]<13)&&(!spfirstfind))
				{	curpos13=m;
					spfirstfind=true;
					
				}
				SPEED__[m]=13;
			}
			spfirstfind=false;
			spfind=false;
			fprintf(file6,"%e;%d;%d;%f;\n",_time[0],curpos6,curpos13,ZMAX);
			
			
			
			cudaMemcpyToSymbol(RAMKA, host_RAMKA, sizeof(host_RAMKA));
				cudaMemcpy  ( g_inuse,host_inuse, (size_t)pf*sizeof(float)*N, cudaMemcpyHostToDevice );
cudaThreadSynchronize();

			fflush(file);
			fflush(file4);
			fflush(file6);
			fflush(file8);
			fflush(file5);
			fflush(file2);
			seconds = time (NULL);
			printf("Time step #: %d ( %e ) , seconds from begin: %ld\n",NfoPr*i,_time[0],(seconds-second0));

	}
	fflush(file5);
	fclose(file5);
	cudaEventDestroy ( start );
	cudaEventDestroy ( stop  );
	cudaFree( cu_x);
	cudaFree( cu_y);
	cudaFree( cu_z);
	cudaFree( cu_a);
	cudaFree( cu_b);
	cudaFree( cu_g);
	cudaFree( cu_ud);
	cudaFree( cu_lr);


	cudaThreadSynchronize();
	cudaFree(cu_rand_seed);
	cudaFree(cu_RandX);
	cudaFree(cu_tdis);
	cudaFree(cu_in1X);
	cudaFree(cu_in2X);
	cudaFree(cu_in3X);
	cudaFree(cu_in4X);
	cudaFree(cu_in5X);
	cudaFree( cu_RandX);


	fclose(file);
	fclose(file2);
	fclose(file3);
	fclose(file4);	
	fclose(file6);
	fclose(file8);

	return 0;
}

